﻿//包基本信息页面
packageContent.baseInfo = function() {
	//初始化包基本信息页面组件
	var _initBasePageComp = function() {
		$('#p_add_startDate').datebox( {
			required : false
		})
		$('#p_add_endDate').datebox( {
			required : false
		})

		$('#imageSelect').window( {
			title : '包图片选择',
			width : 400,
			height : 360,
			modal : true,
			shadow : false,
			closed : true,
			resizable : false,
			draggable : true,
			minimizable : false,
			maximizable : false
		});
		
		//禁用管理地区
		$("#p_add_manageArea").attr("readonly",true);
	};

	var _initBasePageInfo = function() {
		var asd = g_areaName;
		//默认强制复制
		$("#p_add_manageArea").val(g_areaName);
		//基本信息赋值
		if (pack.getPackageBaseInfo() == null) {
			return;
		}

		var packageBaseInfo = pack.getPackageBaseInfo();

		$("#p_add_name").val(packageBaseInfo.getName());
		$("#p_add_code").val(packageBaseInfo.getPackageCode());
		//$("#p_add_manageArea").val(packageBaseInfo.getAreaId());
		$('#p_add_startDate').datebox('setValue',
				packageBaseInfo.getStartDate());
		$('#p_add_endDate').datebox('setValue', packageBaseInfo.getEndDate());
		_setImage(packageBaseInfo.getPackagePicUrl());
		$("#p_add_receipt").val(packageBaseInfo.getReceiptContent());
		$("#p_add_desc").val(packageBaseInfo.getDescription());
		$("#p_add_ad").val(packageBaseInfo.getPackageAD());

	};
	
	var _initPackParamInfo = function() {
		$('#p_param_add_btn').click(function() {
			_addPackageParameter();
		});
		var packParamMap = pack.getPackParamMap();
		if (packParamMap && packParamMap.size() > 0) {
			$.each(packParamMap.values(),function(index,packParam){
				_addPackageParameter(packParam);
			});
		}
		$('#addParamValue').window( {
			title : '参数值添加',
			width : 400,
			height : 145,
			modal : true,
			shadow : false,
			closed : true,
			resizable : false,
			draggable : true,
			minimizable : false,
			maximizable : false
		});
	}

	var _selectImage = function() {
		$('#imageSelect').window('open');
		$('#imageSelect').window('refresh',
				'PackagePathDispatcher?path=selectImageFile.jsp');
	}

	var _setImage = function(path) {
		$("#packagePicUrl").attr('value', path);
		var s = '<img src="../' + path + '" width="50" height="50"/>';
		$("#imageDisplay").html(s);
	}

	//保存
	var _savePackageBaseInfo = function() {
		var _packageBaseInfo = new PackageBaseInfo();
		var _packageInfo = pack.getPackageBaseInfo();
		//保留包ID
		if (_packageInfo != null && _packageInfo != undefined)  {
			_packageBaseInfo.setPackageId(_packageInfo.getPackageId());
		}
		_packageBaseInfo.setName($("#p_add_name").val());
		_packageBaseInfo.setPackageCode($("#p_add_code").val())
		_packageBaseInfo.setAreaId(g_areaId);
		_packageBaseInfo
				.setStartDate($('#p_add_startDate').datebox('getValue'));
		_packageBaseInfo.setEndDate($('#p_add_endDate').datebox('getValue'));
		_packageBaseInfo.setPackagePicUrl($("#packagePicUrl").val());
		_packageBaseInfo.setReceiptContent($("#p_add_receipt").val())
		_packageBaseInfo.setDescription($("#p_add_desc").val())
		_packageBaseInfo.setPackageAD($("#p_add_ad").val());

		if(!Validate.validateBaseInfo(_packageBaseInfo)) {
			return false;
		}
		
		pack.setPackageBaseInfo(_packageBaseInfo);

		//截取包名称 >15
		if (_packageBaseInfo.getName().length > 25) {
			$("#p_config").html(_packageBaseInfo.getName().substr(0,25)+"...")
			$("#p_config").attr('title',_packageBaseInfo.getName());
		} else {
			$("#p_config").html(_packageBaseInfo.getName());
		}
		
		return true;
	}
	
	var _addParamValue = function(paramType, paramId) {
		$('#addParamValue').window('open');
		$('#addParamValue').window('refresh',
				'PackagePathDispatcher?path=addPackParamValue.jsp?paramType=' + paramType + '&paramId=' + paramId);
	}

	var _setDefautValue = function(paramValue, paramDesc, tr) {
		if (Validate.isNull(paramValue)) {return ;}
		
		var valueArr = paramValue.split("|");
		var valueDescArr = paramDesc.split("|");
		var defaultValue = $(tr).find("select[id='param_def_value']");
		$(defaultValue).html("");
		for (var i = 0; i < valueArr.length; i++) {
			var value = valueArr[i];
			var valueDesc = valueDescArr[i];
			var opt = $("<option>").text(valueDesc).val(value);
			defaultValue.append(opt);
		}
	}
	var _getParamValueDesc = function(paramValue, paramType) {
		if (Validate.isNull(paramValue)) {return ;}
		var paramValueDesc = paramValue.trim();
		if (paramType == "S01") {//新装已有不需要
			var valueArr = paramValue.trim().split("|");
			var tempDesc = "";
			$.each(valueArr, function(index, value) {
				if (index == 0) {
					tempDesc = ACTION.PROD_ACTION_TYPE[value];
				} else {
					tempDesc += "|" + ACTION.PROD_ACTION_TYPE[value];
				}
			});
			paramValueDesc = tempDesc;
		} else if (paramType == "S03") {//付费方式
			var valueArr = paramValue.split("|");
			var tempDesc = "";
			$.each(valueArr, function(index, value) {
				if (index == 0) {
					tempDesc = ACTION.FEE_TYPE[value];
				} else {
					tempDesc += "|" + ACTION.FEE_TYPE[value];
				}
			});
			paramValueDesc = tempDesc;
		}
		return paramValueDesc;
	}
	var _addPackageParameter = function(packParam) {
		var packParamId;
		var paramName = "";
		var parameterType = "";
		var optionValue = "";
		var optionValueDesc = "";
		var defaultValue = "";
		if (packParam) {
			packParamId = packParam.getParameterId();
			paramName = packParam.getName();
			parameterType = packParam.getParameterType();
			optionValue = packParam.getOptionValue();
			optionValueDesc = _getParamValueDesc(optionValue, parameterType);
			defaultValue = packParam.getDefaultValue();
		} else {
			packParamId = packageContent.getParameterId();
		}
		
		var packParamTable = $('<table border="0" id="p_param_' + packParamId + '" class="param_table" style="border-bottom:0.1cm groove white">');
		var tr1 = $('<tr><td align="right">参数名称:</td>'
					+ '<td><input type="text" id="param_name"'
					+ 'packParamId="' + packParamId + '" value="' + paramName + '"/></td> '
					+ '<td align="right">参数类型:</td>'
					+ '<td><select id="param_type" packParamId="'+packParamId+'">'
					/***
					+ '<option value="S00">离散值类型（多选一）</option>'
					+ '<option value="S01">产品新装已有类型，可选Y|N|D</option>'
					+ '<option value="S03">设置预后标识</option>'
					**/
					+ '<option value="P03">选址</option>'
					+ '<option value="P05">帐户</option>'
					+ '</select></td></tr>');
		var tr2 = $('<tr><td align="right">参数值:<br/>'
					+ '<img id="p_param_value_add" packParamId="' +packParamId+ '" class="imgAdd" align="right"/>'
					+ '<img id="p_param_value_del" packParamId="' +packParamId+ '" class="imgDel" align="right"/></td>'
					+ '<td colspan="4">'
					+ '<textarea rows="2" id="param_value" readonly="readonly" val="'+ optionValue +'">' + optionValueDesc + '</textarea>'
					+ '</td></tr>');						
		var tr3 = $('<tr><td align="right">默认取值:</td>'
					+ '<td><select id="param_def_value"></select></td>'
					+ '<td colspan="2"><a href="#" style="float:right;margin-right:13px" id="p_param_del" packParamId="' + packParamId + '">删除</a></td></tr>');
		_setDefautValue(optionValue, optionValueDesc, tr3);
		if (parameterType && parameterType != "") {
			$(tr1).find('select[id="param_type"]').val(parameterType);
		}
		packParamTable.append(tr1);
//		packParamTable.append(tr2);
//		packParamTable.append(tr3);
		$('#pack_param_content').append(packParamTable);
		$(packParamTable).find('img[id="p_param_value_add"]').click(function() {
			var paramType = $(this).parents("table").find('select[id="param_type"]').val();
			var packParamId =  $(this).attr("packParamId");
			_addParamValue(paramType, packParamId);
		});
		$(packParamTable).find('img[id="p_param_value_del"]').click(function() {
			var packParamId =  $(this).attr("packParamId");
			_delParamValue(packParamId);
		});
		$(packParamTable).find('a[id="p_param_del"]').click(function() {
			var packParamId =  $(this).attr("packParamId");
			$('table[id="p_param_' + packParamId + '"]').remove();
			var packParamMap = pack.getPackParamMap();
			if (packParamMap) {
				packParamMap.remove(packParamId);
			}
		});
		//参数类型选择事件
		$(packParamTable).find("select[id='param_type']").bind("change", function() {
			var packParamId =  $(this).attr("packParamId");
			
			var paramType = $(this).val();
			if (PACKAGE.PARAMETERTYPE.contentParam.contains(paramType)) {
				var paramTable = $('table[id="p_param_' + packParamId + '"]');
				$(paramTable).find("input[id='param_name']").val($(this).find('option:selected').text());
			} else {
			}
//			_delParamValue(packParamId);
		});
		
		//参数名称赋值
		$(packParamTable).find("input[id='param_name']").val($(packParamTable).find('option:selected').text());
	}
	
	var _delParamValue = function(paramId) {
		var paramTable = $('table[id="p_param_' + paramId + '"]');
		$(paramTable).find("textarea[id='param_value']").html("");
		$(paramTable).find("textarea[id='param_value']").attr('value', "");
		$(paramTable).find("textarea[id='param_value']").attr('val', "");
		$(paramTable).find("select[id='param_def_value']").html("");
	}
	
	var _delPackageParameter = function(obj) {
		
	}
	var _validateParameter = function() {
		var flag = true;
		var paramTypes = [];
		var packParamTables = $('#pack_param_content').find("table");
		$.each(packParamTables,function(i,packParamTable){
			var _paramName = $(packParamTable).find("input[id='param_name']").first().val();
			var _paramValue = $(packParamTable).find("textarea[id='param_value']").first().attr("val");
			var _paramType = $(packParamTable).find("select[id='param_type']").find("option").filter(":selected").val();
			if (paramTypes.contains(_paramType)) {
				alert("相同类型的参数不能添加多条！");
				flag = false;
				return;
			} else {
				paramTypes.push(_paramType);
			}
			if (!_paramName || _paramName == "") {
				alert("【参数名】不能为空！");
				flag = false;
				return;
			} else if (!PACKAGE.PARAMETERTYPE.contentParam.contains(_paramType) && (!_paramName || _paramName == "")) {
				alert("【参数值】不能为空！");
				flag = false;
				return;
			}
			if (_paramName.length > 20) {
				alert("【参数名】不能大于20字符！");
				flag = false;
				return;
			}
		});
		return flag;
	}
	var _savePackageParameter = function() {
		if (!_validateParameter()) {
			return false;
		}
		var packParamMap = pack.getPackParamMap();
		if (!packParamMap) {
			packParamMap = new HashMap();
		}
		var packParamTables = $('#pack_param_content').find("table");
		$.each(packParamTables,function(i,packParamTable){
			var _packParam = new PackageParameter();
			var packParamId = $(packParamTable).find("input[id='param_name']").attr("packParamId");
			var paramName = $(packParamTable).find("input[id='param_name']").first().val();
			var paramValue = $(packParamTable).find("textarea[id='param_value']").first().attr("val");
			var paramType = $(packParamTable).find("select[id='param_type']").find("option").filter(":selected").val();
			var defaultValue = $(packParamTable).find("select[id='param_def_value']").find("option").filter(":selected").val();
			_packParam.setParameterId(packParamId);
			_packParam.setName(paramName);
			_packParam.setParameterType(paramType);
			_packParam.setOptionValue(paramValue);
			_packParam.setDefaultValue(defaultValue);
			packParamMap.put(packParamId, _packParam);
		});
		pack.setPackParamMap(packParamMap);
		return true;
	}
	
	//关闭
	var _doClose = function() {
		$("#openBaseInfoPage").window('close');
	}

	//初始化包基本信息
	var _init = function() {
		_initBasePageComp();
		_initBasePageInfo();
		_initPackParamInfo();

		$("#p_setImage").bind("click", function() {
			_selectImage();
		});

		$("#p_base_save").bind("click", function() {
			var flag = _savePackageBaseInfo();
			if (flag) {
				flag = _savePackageParameter();
				if (flag) {
					_doClose();
				}
			}
		});
		$("#p_base_cancel").bind("click", function() {
			_doClose();
		});
	}
	_init();
}
